package com.hll.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hll.api.exception.MyException;
import com.hll.api.service.WallpaperApi;
import com.hll.api.util.HttpUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 何龙
 * @create 2019-06-03 15:24
 */
@Service
public class WallpaperApiImpl implements WallpaperApi {
	@Override
	public JSONArray getType() throws Exception {
		String s = HttpUtils.get("http://cdn.apc.360.cn/index.php?c=WallPaper&a=getAllCategoriesV2&from=360chrome", null, 0, 0);
		JSONObject jsonObject = JSON.parseObject(s);
		if ("正常".equals(jsonObject.getString("errmsg"))) {
			return jsonObject.getJSONArray("data");
		} else {
			throw new MyException(jsonObject.getString("errmsg"));
		}
	}

	@Override
	public JSONObject getImageList(String cid, Integer start, Integer count) throws Exception {
		HashMap<String, Object> map = new HashMap<>(5);
		map.put("c", "WallPaper");
		map.put("a", "getAppsByCategory");
		map.put("cid", cid);
		map.put("start", start);
		map.put("count", count);

		String s = HttpUtils.get("http://wallpaper.apc.360.cn/index.php", map, 0, 0);
		JSONObject jsonObject = JSON.parseObject(s);
		if ("正常".equals(jsonObject.getString("errmsg"))) {
			JSONObject d = new JSONObject();
			d.put("total", Long.valueOf(jsonObject.getString("total")));
			JSONArray data = jsonObject.getJSONArray("data");
			JSONArray array = new JSONArray();
			for (Object datum : data) {
				JSONObject o = (JSONObject) datum;
				Map<String, Object> mp = new HashMap<>(10);
				mp.put("utag", o.getString("utag"));
				mp.put("update_time", o.getString("update_time"));
				mp.put("url_mobile", o.getString("url_mobile"));
				mp.put("url", o.getString("url"));
				mp.put("urlurl_thumb", o.getString("url_thumb"));
				mp.put("url_mid", o.getString("url_mid"));
				mp.put("img_1600_900", o.getString("img_1600_900"));
				mp.put("img_1440_900", o.getString("img_1440_900"));
				mp.put("img_1366_768", o.getString("img_1366_768"));
				mp.put("img_1280_800", o.getString("img_1280_800"));
				mp.put("img_1280_1024", o.getString("img_1280_1024"));
				mp.put("img_1024_768", o.getString("img_1024_768"));
				array.add(mp);
			}
			d.put("list", array);
			return d;
		} else {
			throw new MyException(jsonObject.getString("errmsg"));
		}
	}
}
